package com.ruoyi.cms.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.cms.domain.CmsArticle;
import com.ruoyi.cms.domain.CmsArticleInputFile;
import com.ruoyi.cms.domain.CmsArticleInputFileDetail;
import com.ruoyi.cms.domain.CmsImage;
import com.ruoyi.cms.mapper.CmsArticleInputFileDetailMapper;
import com.ruoyi.cms.mapper.CmsArticleMapper;
import com.ruoyi.cms.mapper.CmsImageMapper;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class CmsArticleFileService {

    @Autowired
    CmsArticleInputFileDetailMapper cmsArticleInputFileDetailMapper;

    @Autowired
    CmsArticleMapper cmsArticleMapper;

//    @Autowired
//    ICmsArticleService iCmsArticleService;
//
//    @Autowired
//    ICmsArticleInputFileDetailService iCmsArticleInputFileDetailService;

    public int input(File file, CmsArticleInputFile cmsArticleInputFile) throws FileNotFoundException {
        ExcelUtil<CmsArticleInputFileDetail> excelUtil = new ExcelUtil<>(CmsArticleInputFileDetail.class);
        List<CmsArticleInputFileDetail> list = excelUtil.importExcel(new FileInputStream(file));
//        List<CmsArticle> articles = new ArrayList<>();
//        List<CmsArticleInputFileDetail> details = new ArrayList<>();
        for (CmsArticleInputFileDetail temp : list) {
            temp.setCreateTime(new Date());
            temp.setArticleInputFileId(cmsArticleInputFile.getId());
            System.out.println(temp);

            CmsArticle article = covertToCmsArticle(temp);
//            articles.add(article);
//            details.add(temp);
            cmsArticleMapper.insertCmsArticle(article);
            cmsArticleInputFileDetailMapper.insertCmsArticleInputFileDetail(temp);
        }
//        iCmsArticleService.

        return 1;
    }

    @Autowired
    CmsImageMapper cmsImageMapper;
    private CmsArticle covertToCmsArticle(CmsArticleInputFileDetail detail) {
        CmsArticle cmsArticle = new CmsArticle();
        try {

            JSONArray imageArray = JSONArray.parseArray(detail.getImgList());
            List<String> imgList = new ArrayList<>();
            for(int i = 0; i < imageArray.size(); i++) {
                String localFilePath = imageArray.getString(i);
                CmsImage cmsImage = new CmsImage();
                cmsImage.setLocalFilePath(localFilePath);
                List<CmsImage> tempImgList = cmsImageMapper.selectCmsImageList(cmsImage);
                if(CollectionUtils.isNotEmpty(tempImgList)) {
                    imgList.add(tempImgList.get(0).getUrl());
                }
            }

            cmsArticle.setTitle(detail.getTitle());
            cmsArticle.setKeyword(detail.getKeyword());
            cmsArticle.setAvatar(CollectionUtils.isNotEmpty(imgList) ? imgList.get(0) : null);
            cmsArticle.setBrief(detail.getTitle());
            cmsArticle.setShareUrl(detail.getShareUrl());
            cmsArticle.setShareCode(detail.getShareCode());
            cmsArticle.setImgList(detail.getImgList());
            cmsArticle.setCreateTime(detail.getCreateTime());

            String aiContent = detail.getAiContent();
            StringBuffer sb = new StringBuffer();
            aiContent = "<p>" + aiContent.replaceAll("\n\n", "</p><br/><p>") + "</p><br/>";
            sb.append(aiContent);
            cmsArticle.setAiContent(sb.toString());

            if(CollectionUtils.isNotEmpty(imgList)) {
                sb.append("<p><br></p><p>");
                for(String img : imgList) {
                    sb.append("<img src=\"").append(img).append("\">");
                }
                sb.append("</p><p><br/></p>");
            }
            sb.append("<p style=\"color:red;font-size:20px\" >中文破解版链接，解压即玩：").append(detail.getShareUrl()).append("</p>");
            sb.append("<br>");
            sb.append("<p style=\"color:red;font-size:20px\" >怎么获取提取码：<br/> 1.点击下面赞赏按钮，选择金额，截屏后用微信扫一扫 <br/> 2. 去消息模块查看提取码 <br/>您的支持将是我们莫大的荣幸与感激！！</p><p><br/></p><p><br/></p><p><br/></p>");

            cmsArticle.setContent(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
            detail.setInputStatus(0);
        }
        return cmsArticle;
    }
}
